<script>
import PostBase from '@/components/post/PostBase.vue'
import { apiGetPosts } from '@/api/space.js'
export default {
  name: 'SpacePosts',
  extends: PostBase,
  props: {
    mid: {
      type: [Number, String],
      required: true
    }
  },
  data () {
    return {
      // 当前展示记录的最后一个id 用来发送分页获取数据的参数
      nextOffset: null
    }
  },
  created () {
    this.getData()
  },
  watch: {
    mid: {
      handler (newVal) {
        this.resetListStatus()
        this.source = []
        this.nextOffset = null
        this.getData()
      }
    }
  },
  methods: {
    getData () {
      apiGetPosts({
        hostUid: this.mid,
        offsetDynamicId: this.nextOffset ? this.nextOffset : ''
      }).then(res => {
        const { code, data } = res
        if (code === 0 && data) {
          const { cards } = data
          this.nextOffset = cards[cards.length - 1].desc.dynamic_id_str
          this.source.push(...cards)
          if (data.has_more !== 0) {
            this.listFinished = false
          } else {
            this.listFinished = true
          }
        } else {
          this.listFinished = true
        }
      }).catch(err => {
        console.log(err)
        this.listError = true
      }).finally(() => {
        this.listLoading = false
      })
    }
  }
}
</script>
